<?php
session_start();
//包含头
include '../PHP/head.php';
?>
<?php
//包含bootstrap的顶端按钮
include '../PHP/menu.php';
?>


<br />
<br />
<p class="text-center lead">模块报价编号编辑</p>
<?php
//如果查到GET则，显示表单并显示原有数据；
//如果查POST则更新数据；
//引用必要的函数
require_once ('../FNS/db_fns.php');
if(isset($_GET['project_id'])) {
	//去查到这个项目
	//注意表单中需要一个隐藏的project_id
	$project_id = trim($_GET['project_id']);
	
	//数据过滤和处理

	if (! get_magic_quotes_gpc()) {
	$project_id=addslashes ($project_id);
	}
	
	$db = db_connect();
	$query="select projects.code as project_code, projects.name as project_name, projects.sale as sale_id, users.name as sale_name from projects inner join users on projects.sale = users.id where projects.id = '".$project_id."'";
	
	$result=$db->query ($query);
	$num_result=$result->num_rows;
	
	//拿出数据
	$row=$result->fetch_assoc ();
	$project_code = stripslashes ($row['project_code']);
	$project_name = stripslashes ($row['project_name']);
	$sale_name = stripslashes ($row['sale_name']);
	$sale_id = stripslashes ($row['sale_id']);
	
	$result->free();
	
	
	echo "
	<form class='form-inline' action = '../PHP/edit_project_code.php' method = 'post'>
		<input type='hidden' name = 'project_id' value = '".$project_id."'>
		<div class='form-group'>
			<label for='project_code'>模块报价编号：</label>
			<input type = 'text'  class='form-control' name = 'project_code' value= '".$project_code."'/>
		</div>
		<div class='form-group'>
			<label for='project_name'>项目名称：</label>
			<input type = 'text'  class='form-control' name = 'project_name' value = '".$project_name."'/>
		</div>
 
		<div class='form-group'>
			<label for='sale'>销售员：</label>
			<select class='form-control' name='sale'>
	";
	//先输出当前项目销售员
	//如果项目在进行中，销售员修改了=>所有操作换一个销售来操作。那就修改吧~~~
	
	echo "<option value='".$sale_id."'>".$sale_name."</option>";
	//去搜销售员
	$query="select * from users where position like '%SA%'";
	$result=$db->query ($query);

	//获取记录数量
	$num_results=$result->num_rows;
	
	//循环输出选项
	for ($i=0; $i < $num_results; $i++) {
 
		$row=$result->fetch_assoc ();
	
		$user_id = stripslashes ($row['id']);
		$user_name = stripslashes ($row['name']);
	
		echo "<option value='".$user_id."'>".$user_name."</option>";
	}

$result->free();
$db->close();
echo "
	</select>
	</div>
	<button type='submit' class='btn btn-success'>修改</button>
	</form>
	";
}

?>

<?php
if(isset($_POST['project_id'])) {
	
	$project_id = trim($_POST['project_id']);
	$project_code = trim ($_POST['project_code']);
	$project_name = trim ($_POST['project_name']);
	$sale = trim ($_POST['sale']);
	
	//判断是否为空
	if (!$project_code || !$project_name || !$sale){
		echo "<br />";
		echo "<p>所有输入不能为空，数据未更新！</P>";
		echo "</div>";
		echo "</body>";
		echo "</html>";
		exit;
	}
	//数据过滤和处理
	//加引号避免输入数据库出错

	if (! get_magic_quotes_gpc()) {
	$project_id=addslashes ($project_id);
	$project_code=addslashes ($project_code);
	$project_name=addslashes ($project_name);
	$sale=addslashes ($sale);
	}

	//连接数据库并插入数据

	$db= db_connect();

	$query="update projects set code = '".$project_code."', name = '".$project_name."', sale = '".$sale."' where id = '".$project_id."'";
$result=$db->query ($query);
if ($result){
	echo "<br />";
	echo "修改成功，请重新查询确认。";
}else{
	echo "<br />";
	echo"修改失败！";
}


$db->close();
	
}


?>


</div>
</body>
</html>
